package com.app.business.show;

import com.app.business.model.*;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Response;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 大屏公示2（没有权限限制）
 * Created by ddz on 2022/10/29.
 */
@Controller
@RequestMapping("/showForBs3/*")
@SuppressWarnings("all")
public class ShowForBs3Controller {
    @Autowired
    private ContractService contractService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private ShowHjService showHjService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private ShowForHjTwoController showForHjTwoController;

    /**
     * 大屏主界面
     */
    @RequestMapping("tv")
    public void tv(ModelMap modelMap) {
    }

    /**
     * 选房家庭公示
     */
    @RequestMapping("contractShowMain")
    public void contractShowMain(ModelMap modelMap) {
        Map<String, String> townMap = this.dataDictService.getMapByParentNameForValue_Name("乡镇");
        Map<String, List<ZsProject>> zsProjectMap = this.zsProjectService.getZsProjectMap();
        modelMap.addAttribute("townMap", townMap);
        modelMap.addAttribute("zsProjectMap", zsProjectMap);
    }

    /**
     * 得到某些征收项目下对应的安置项目
     *
     * @param request
     * @return
     */
    @RequestMapping("getAzProjectListByZsProjectIdStr")
    @ResponseBody
    public Response getAzProjectListByZsProjectIdStr(HttpServletRequest request) {
        String zsProjectIdStr = ParamUtils.getString(request, "zsProjectIdStr", "");
        List<AzProject> azProjectList = this.azProjectService.getAzProjectListByZsProjectIdStr(zsProjectIdStr);
        Response response = new Response();
        response.setData(azProjectList);
        response.setCode("0001");
        response.setMessage("数据加载成功!");
        return response;
    }

    /**
     * 选房家庭公示
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("contractShow")
    public void contractShow(HttpServletRequest request, ModelMap modelMap) {
        String zsProjectIdStr = ParamUtils.getString(request, "zsProjectIdStr", "");
        String azProjectIdStr = ParamUtils.getString(request, "azProjectIdStr", "");
        List<ZsProject> zsProjectList = this.zsProjectService.getZsProjectListByZsProjectIdStr(zsProjectIdStr);
        List<AzProject> azProjectList = this.azProjectService.getAzProjectListByAzProjectIdStr(azProjectIdStr);

        Map<String, AzProject> azProjectMap = this.azProjectService.getAzProjectMap();

        modelMap.addAttribute("zsProjectIdStr", zsProjectIdStr);
        modelMap.addAttribute("azProjectIdStr", azProjectIdStr);
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("azProjectMap", azProjectMap);

    }

    /**
     * 选房家庭公示
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("contractShowForHigh")
    public void contractShowForHigh(HttpServletRequest request, ModelMap modelMap) {
        String zsProjectIdStr = ParamUtils.getString(request, "zsProjectIdStr", "");
        String azProjectIdStr = ParamUtils.getString(request, "azProjectIdStr", "");
        List<ZsProject> zsProjectList = this.zsProjectService.getZsProjectListByZsProjectIdStr(zsProjectIdStr);
        List<AzProject> azProjectList = this.azProjectService.getAzProjectListByAzProjectIdStr(azProjectIdStr);

        Map<String, AzProject> azProjectMap = this.azProjectService.getAzProjectMap();

        modelMap.addAttribute("zsProjectIdStr", zsProjectIdStr);
        modelMap.addAttribute("azProjectIdStr", azProjectIdStr);
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("azProjectList", azProjectList);
        modelMap.addAttribute("azProjectMap", azProjectMap);

    }


    /**
     * 获取房源数据
     *
     * @param request
     * @return
     */
    @RequestMapping("houseHoldMapData")
    public void houseHoldMapData(HttpServletRequest request, ModelMap modelMap) {
        String zsProjectIdStr = ParamUtils.getString(request, "zsProjectIdStr", "");
        String azProjectIdStr = ParamUtils.getString(request, "azProjectIdStr", "");
        Map<String, Map<String, Map<String, Map>>> houseHoldDataMap = this.houseService.getHouseHoldMap(zsProjectIdStr, azProjectIdStr);
        Map<String, String> houseHoldMap = this.dataDictService.getMapByParentNameForValue_Name("房屋户型");
        Map<String, AzProject> azProjectMap = this.azProjectService.getAzProjectMap();
        modelMap.addAttribute("houseHoldDataMap", houseHoldDataMap);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("azProjectMap", azProjectMap);
    }

    /**
     * 选房家庭明细
     *
     * @param request
     * @return
     */
    @RequestMapping("getChooseHouseInfo")
    @ResponseBody
    public Map getChooseHouseInfo(HttpServletRequest request) {
        String zsProjectIdStr = ParamUtils.getString(request, "zsProjectIdStr", "");
        String azProjectIdStr = ParamUtils.getString(request, "azProjectIdStr", "");
        Integer pageSize = ParamUtils.getInt(request, "pageSize", 30);
        Integer pageNo = ParamUtils.getInt(request, "pageNo", 1);
        PageBean<Contract> pageBean = new PageBean<Contract>();
        pageBean = this.contractService.getChoosedHouseContractPageBean(zsProjectIdStr, azProjectIdStr, pageSize, pageNo);
        List<Map> dataLists = Lists.newLinkedList();
        Map<Integer, List<House>> houseMap = Maps.newHashMap();
        Map<Integer, List<CarLot>> carLotMap = Maps.newHashMap();
        Map<Integer, List<StorageRoom>> storageRoomMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(pageBean.getDatas())) {
            List<Integer> contractIdList = Lists.newArrayList();
            for (Contract contract : pageBean.getDatas()) {
                contractIdList.add(contract.getId());
            }
            houseMap = this.houseService.getHouseMapByContractIdList(contractIdList);
            carLotMap = this.carLotService.getCarLotMapByContractIdList(contractIdList);
            storageRoomMap = this.storageRoomService.getStorageRoomMapByContractIdList(contractIdList);
        }

        if (CollectionUtils.isNotEmpty(pageBean.getDatas())) {
            Map map = null;
            String strInfo;
            String houseStr;
            String carLotStr;
            String storageRoomStr;
            String syAreaStr;
            for (Contract contract : pageBean.getDatas()) {
                map = null;
                houseStr = "";
                carLotStr = "";
                storageRoomStr = "";
                syAreaStr = "";
                map = Maps.newHashMap();
                map.put("printTableName", StringUtils.getNotNull(contract.getZsProject().getPrintTableName()));
                map.put("name", StringUtils.getNotNull(contract.getName()));
                map.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                BigDecimal syArea = new BigDecimal("0");
                if (contract.getIsJiSuan() == 2) {
                    syArea = this.contractService.getSyAreaByAzContractId(contract.getId());
                }
                if (CollectionUtils.isNotEmpty(houseMap.get(contract.getId()))) {
                    for (House house : houseMap.get(contract.getId())) {
                        houseStr += house.getAzProject().getMemo() + house.getHouseNumStr() + ",";
                    }
                }
                if (CollectionUtils.isNotEmpty(carLotMap.get(contract.getId()))) {
                    for (CarLot carLot : carLotMap.get(contract.getId())) {
                        carLotStr += carLot.getAzProject().getMemo() + carLot.getHouseNumStr() + ",";
                    }
                }
                if (CollectionUtils.isNotEmpty(storageRoomMap.get(contract.getId()))) {
                    for (StorageRoom storageRoom : storageRoomMap.get(contract.getId())) {
                        storageRoomStr += storageRoom.getHouseNumStr() + ",";
                    }
                }
                map.put("strInfo", houseStr + carLotStr + storageRoomStr);
                map.put("houseStrInfo", houseStr);
                map.put("carLotStrInfo", carLotStr);
                map.put("chooseHouseDate", DateUtils.dateToStringFormat(contract.getChooseHouseDate(), "yyyy-MM-dd HH:mm"));
                dataLists.add(map);
            }
        }
        Map resultMap = Maps.newHashMap();
        resultMap.put("totalCount", pageBean.getTotalCount());
        resultMap.put("dataLists", dataLists);

        Date nowDate = new Date();
        if (DateUtils.stringToDateTimeCompare(nowDate).equals("上午")) {
            resultMap.put("timeStr", DateUtils.dateToStringDefault(nowDate) + "上午已选房情况");
        } else if (DateUtils.stringToDateTimeCompare(nowDate).equals("下午")) {
            resultMap.put("timeStr", DateUtils.dateToStringDefault(nowDate) + "下午已选房情况");
        }
        return resultMap;
    }

    /**
     * 楼盘图
     *
     * @param request
     */
    @RequestMapping("housePic")
    public void housePic(HttpServletRequest request, ModelMap modelMap) {
        String zsProjectIdStr = ParamUtils.getString(request, "zsProjectIdStr", "");
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        String houseStyle = ParamUtils.getString(request, "houseStyle", "");
        String houseHold = ParamUtils.getString(request, "houseHold", "");
        String houseArea = ParamUtils.getString(request, "houseArea", "");

        //通过征收项目、安置项目、楼号得到所分配的houseList
        Map<String, List> houseMap = showHjService.houseMapFoHjThree(zsProjectIdStr, azProjectId, houseHold, houseStyle, houseArea, null);
        Set set = houseMap.keySet();
        //房源状态
        Map<String, String> colorMap = this.dataDictService.getMapByParentNameForValue_Color("公示颜色");
        //房屋类型
        Map<String, String> houseHoldMap = this.dataDictService.getMapByParentNameForValue_Name("房屋户型");
        modelMap.addAttribute("colorMap", colorMap);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        List<String> buildNumList = showHjService.buildNumListFoHjThree(zsProjectIdStr, azProjectId, houseHold, houseStyle, houseArea, null);
        modelMap.addAttribute("buildNumList", buildNumList);
        modelMap.addAttribute("houseMap", houseMap);
        modelMap.addAttribute("strUtils", new StringUtils());
    }


    /**
     * 湖滨二期
     */
    @RequestMapping("houseShowBs3")
    public void houseShowJx1(HttpServletRequest request, ModelMap modelMap) {
        Map<String, String> colorMap = this.dataDictService.getMapByParentNameForValue_Color("公示颜色");
        modelMap.addAttribute("colorMap", colorMap);
    }

    /**
     * 湖滨二期
     */
    @RequestMapping("houseShowBs3High")
    public void houseShowBs3High(HttpServletRequest request, ModelMap modelMap) {
        Map<String, String> colorMap = this.dataDictService.getMapByParentNameForValue_Color("公示颜色");
        modelMap.addAttribute("colorMap", colorMap);
    }
    /**
     * 楼盘图
     *
     * @param request
     */
    @RequestMapping("housePicHighAll")
    public void housePicHighAll(HttpServletRequest request, ModelMap modelMap) {
        this.housePic(request, modelMap);
    }

    /**
     * 楼盘图
     *
     * @param request
     */
    @RequestMapping("housePicAll")
    public void housePicAll(HttpServletRequest request, ModelMap modelMap) {
        this.housePic(request, modelMap);
    }

    /**
     * 楼盘图
     *
     * @param request
     */
    @RequestMapping("housePicAllForFs")
    public void housePicAllForFs(HttpServletRequest request, ModelMap modelMap) {
        this.showForHjTwoController.housePic(request, modelMap);
    }
}
